/**
 * request 封装 （在客户端调用）
 * Created by wusai on 20/9/15
 */
import _ from 'lodash'
// import urlConfig from '@/common/js/url-config' // 接口路径封装
import { baseUrl } from '@/common/js/config' // 接口域名

let options = {
	timeout: 10000,
}
let header = {
	'Content-Type': 'application/json;charset=utf-8'
}

function requestPost(url, param = '', option = {}) {
	// Main.defaults.headers.common['Authorization'] 
	
	header = Object.assign(header,option.header,{
		'ACCESS-TOKEN': param['ACCESS-TOKEN']
	})
	let newOption = Object.assign(options, {'header': header}, delete(option['header']))
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'POST',
			url: baseUrl + url,
			data: param,
			// header: header,
			...newOption,
			success: (res) => {
				let [pages,curPage] = ['',''] // 通过H5跳进来直接在这个页面登录并添加球员，不需要跳转回我的
				if(url.includes('player/queryPlayerListByUser')){
					pages = getCurrentPages()
					curPage = pages[pages.length - 1]
				}
				if(res.data.code==401&&(!url.includes('player/queryPlayerListByUser')&&!curPage.includes('clubId='))){
					uni.setStorageSync('needLogin', true)
					uni.switchTab({
						url: '/pages/personalCenter/index/index'
					})
				}
				resolve({
				  data:res.data.data,
				  code:res.data.code,
				  message:res.data.message,
				  errMsg:res.errMsg,
				  statusCode:res.statusCode
			  })
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

function requestUpload(url, param) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseUrl + url,//
			filePath: param['imgurl'],
			name: 'file',
			header: {
				'ACCESS-TOKEN': param['ACCESS-TOKEN'],
				'content-type': 'multipart/form-data'
			},
			formData: param.fmData || null,
			success: function (res) {
				let data = JSON.parse(res.data)
				resolve({
					data:data.data || {},
					code:data.code,
					message:data.message,
					errMsg:res.errMsg,
					statusCode:res.statusCode
				})
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

function requestGet(url, param = '', option = {}) {
	let token = uni.getStorageSync('accessToken')
	if (param !== '') {
		let newParam = ''
		_.each(param, (value, key) => {
			if (key !== 'ACCESS-TOKEN' && key !== 'CODE-KEY') {
				if (newParam === '') {
					newParam = '?' + key + '=' + value
				} else {
					newParam += '&' + key + '=' + value
				}
			}
		})
		url = url + newParam
	}
	header = Object.assign(header,option.header,{
		'ACCESS-TOKEN': param['ACCESS-TOKEN']
	})
	let newOption = Object.assign(options, {'header': header}, delete(option['header']))
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'GET',
			url: baseUrl + url,
			// data: param,
			// header: header,
			...newOption,
			success: (res) => {
				let [pages,curPage] = ['',''] // 通过H5跳进来直接在这个页面登录并添加球员，不需要跳转回我的
				if(url.includes('player/list')){
					pages = getCurrentPages()
					curPage = pages[pages.length - 1]
				}
				if(res.data.code==401&&!url.includes('order/count/get')&&!url.includes('user/get')&&!url.includes('member/getApprove')&&(!url.includes('player/list')&&!curPage.includes('clubId='))){
					uni.setStorageSync('needLogin', true)
					uni.switchTab({
						url: '/pages/personalCenter/index/index'
					})
				}
				resolve({
				  data:res.data.data,
				  code:res.data.code,
				  message:res.data.message,
				  errMsg:res.errMsg,
				  statusCode:res.statusCode
			  })
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

module.exports = {
	requestGet,
	requestPost,
	requestUpload
}
